package 多线程.Test;

class Counter implements Runnable{
   private int count;
 
   public Counter() {
      count = 0;
   }
 
   public void countAdd() {
      synchronized(this) {
         for (int i = 0; i < 5; i ++) {
            try {
               System.out.println(Thread.currentThread().getName() + ":" + (count++));
               Thread.sleep(100);
            } catch (Exception e) {
               e.printStackTrace();
            }
         }
      }
   }
 
   //非synchronized代码块，未对count进行读写操作，所以可以不用synchronized
   public void printCount() {
      for (int i = 0; i < 5; i ++) {
         try {
            System.out.println(Thread.currentThread().getName() + " count:" + count);
            Thread.sleep(100);
         } catch (InterruptedException e) {
            e.printStackTrace();
         }
      }
   }
 
   public void run() {
      String threadName = Thread.currentThread().getName();
      if (threadName.equals("A") || threadName.equals("C")) {
         countAdd();
      } else if (threadName.equals("B")) {
         printCount();
      }
   }
}

/**
 * 可以看见B线程的调用是非synchronized,并不影响A线程对synchronized部分的调用。
 * 从上面的结果中可以看出一个线程访问一个对象的synchronized代码块时，
 * 别的线程可以访问该对象的非synchronized代码块而不受阻塞。
 */
public class Demo01{
    public static void main(String args[]){
        Counter counter = new Counter();
//       Counter counter2 = new Counter();
        Thread thread1 = new Thread(counter, "A");
        Thread thread2 = new Thread(counter, "B");
       Thread thread3 = new Thread(counter, "C");
        thread1.start();
//        thread2.start();
       thread3.start();
    }
}